﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;


namespace GestorDeFlotasDesktop.AbmTurno
{
   
    
    public partial class ListadoTurno : Form
    {
        Boolean modificacion;
        Boolean baja;
        
        String conexion;
        public ListadoTurno()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            dataGridView1.Enabled = false;
        }

        public void setearFormModificacion()
        {
            this.modificacion = true;
            this.baja = false;
        }

        public void setearFormBaja()
        {
            this.modificacion = false;
            this.baja = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Enabled = true;
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {

                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_buscarTurno";
                        command.Parameters.Add(new SqlParameter("@Descripcion", descripcionTextBox.Text));
                        
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataSet ds = new DataSet();
                            adapter.Fill(ds);
                            dataGridView1.DataSource = ds.Tables[0];

                        }


                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
                this.ejecutarSeleccion(e);
        }

        private void ejecutarSeleccion(DataGridViewCellEventArgs e)
        {
            int idTurno = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
            String horaInicio = (Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value)).ToString();
            String horaFin = (Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value)).ToString();
            String descripcion = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            String valorFicha = (Convert.ToInt32(dataGridView1.CurrentRow.Cells[5].Value)).ToString();
            String valorBandera = (Convert.ToInt32(dataGridView1.CurrentRow.Cells[6].Value)).ToString();
            Boolean habilitado;
            if(dataGridView1.CurrentRow.Cells[7].Value != DBNull.Value){
             habilitado = Convert.ToBoolean( dataGridView1.CurrentRow.Cells[7].Value);
            }else{
              habilitado = false;
            }
                
            if (this.baja)
            {
                BajaTurno baja = new BajaTurno();
                baja.mostrateConCampos(idTurno,horaInicio,horaFin,descripcion,valorFicha,valorBandera,habilitado);
            }
            else
            {
                ModificacionTurno modificacion = new ModificacionTurno();
                modificacion.mostrateConCampos(idTurno, horaInicio, horaFin, descripcion, valorFicha, valorBandera, habilitado);
            }
                        
        }

        private void limpiarButton_Click(object sender, EventArgs e)
        {
            descripcionTextBox.Text = "";
        }

    }
}
